#!/usr/bin/ python3
# -*- coding: utf-8 -*-
from sici import *
from chaojiying import *
from sendmail import * 
from selenium import webdriver
from PIL import Image
import time
option = webdriver.ChromeOptions()
option.add_argument('headless')
option.add_argument('no-sandbox')
option.add_argument('disable-dev-shm-usage')
browser = webdriver.Chrome('/usr/local/bin/chromedriver',chrome_options=option)
# browser = webdriver.Chrome()
browser.get('http://www.yiban.cn/login?go=https://q.yiban.cn/app/index/appid/655895')
browser.implicitly_wait(7)
input_tag = browser.find_element_by_id('account-txt')
time.sleep(4)
input_tag.send_keys('user')
time.sleep(4)
input_tag = browser.find_element_by_id('password-txt')
time.sleep(4)
input_tag.send_keys('password')
time.sleep(4)
browser.find_element_by_id('login-btn').click()
time.sleep(8)
currentPageUrl = browser.current_url
if currentPageUrl == "https://q.yiban.cn/app/index/appid/655895":
	try:
		browser.find_element_by_link_text("签到").click()
		time.sleep(4)
		print("签到成功")
	except:
		print("已经签到")
	browser.find_element_by_xpath('/html/body/main/article/section[3]/div/div/div/div/div/div/div/div/div[1]/div[1]/div/textarea').click()
	time.sleep(1)
	browser.find_element_by_xpath('/html/body/main/article/section[3]/div/div/div/div/div/div/div/div/div[1]/div[1]/div/textarea').send_keys(poetry())
	button_tag = browser.find_element_by_class_name('comment-submit')
	time.sleep(1)
	button_tag.click()
	time.sleep(5)
	print("回复成功")
	ret=mail()
	if ret:
		print("邮件发送成功")
	else:
		print("邮件发送失败")	
else:
	browser.save_screenshot('bdbutton.png')
	find_code = browser.find_element_by_xpath('//*[@id="login-box"]/div[3]/img')    #找到验证码图片
	left = find_code.location['x']
	top = find_code.location['y']
	right = find_code.location['x'] + find_code.size['width']
	bottom = find_code.location['y'] + find_code.size['height']
	im = Image.open('bdbutton.png')
	im = im.crop((left, top, right, bottom))
	im.save('bdbutton.png')
	print("验证码获取成功")#验证码获取模块
	#超级鹰识别模块
	chaojiying = Chaojiying_Client('账号', '密码', '应用id')	#用户中心>>软件ID 生成一个替换 96001
	im = open('bdbutton.png', 'rb').read()												#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
	# print(chaojiying.PostPic(im, 2001)
	print("识别成功")
	input_tag = browser.find_element_by_id('login-captcha')
	input_tag.click()
	input_tag.send_keys(chaojiying.PostPic(im, 2001))
	browser.save_screenshot('failed.png')
	browser.find_element_by_id('login-btn').click()
	time.sleep(8)
	currentPageUrl = browser.current_url
	if currentPageUrl == "https://q.yiban.cn/app/index/appid/655895":
		try:
			browser.find_element_by_link_text("签到").click()
			time.sleep(4)
			print("签到成功")
		except:
			print("已经签到")
		browser.find_element_by_xpath('/html/body/main/article/section[3]/div/div/div/div/div/div/div/div/div[1]/div[1]/div/textarea').click()
		time.sleep(1)
		browser.find_element_by_xpath('/html/body/main/article/section[3]/div/div/div/div/div/div/div/div/div[1]/div[1]/div/textarea').send_keys(poetry())
		button_tag = browser.find_element_by_class_name('comment-submit')
		time.sleep(1)
		button_tag.click()
		time.sleep(5)
		print("回复成功")
		ret=mail()
		if ret:
			print("邮件发送成功")
		else:
			print("邮件发送失败")	
	else:
		browser.save_screenshot('bdbutton.png')
		find_code = browser.find_element_by_xpath('//*[@id="login-box"]/div[3]/img')    #找到验证码图片
		left = find_code.location['x']
		top = find_code.location['y']
		right = find_code.location['x'] + find_code.size['width']
		bottom = find_code.location['y'] + find_code.size['height']
		im = Image.open('bdbutton.png')
		im = im.crop((left, top, right, bottom))
		im.save('bdbutton.png')
		print("验证码获取成功")#验证码获取模块
		#超级鹰识别模块
		chaojiying = Chaojiying_Client('账号', '密码', '应用id')	#用户中心>>软件ID 生成一个替换 96001
		im = open('bdbutton.png', 'rb').read()												#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
		# print(chaojiying.PostPic(im, 2001)
		print("识别成功")
		input_tag = browser.find_element_by_id('login-captcha')
		input_tag.click()
		input_tag.send_keys(chaojiying.PostPic(im, 2001))
		browser.save_screenshot('failed.png')
		browser.find_element_by_id('login-btn').click()
		time.sleep(8)
		currentPageUrl = browser.current_url
		if currentPageUrl == "https://q.yiban.cn/app/index/appid/655895":
			try:
				browser.find_element_by_link_text("签到").click()
				time.sleep(4)
				print("签到成功")
			except:
				print("已经签到")
				browser.find_element_by_xpath('/html/body/main/article/section[3]/div/div/div/div/div/div/div/div/div[1]/div[1]/div/textarea').click()
				time.sleep(1)
				browser.find_element_by_xpath('/html/body/main/article/section[3]/div/div/div/div/div/div/div/div/div[1]/div[1]/div/textarea').send_keys(poetry())
				button_tag = browser.find_element_by_class_name('comment-submit')
				time.sleep(1)
				button_tag.click()
				time.sleep(5)
				print("回复成功")
				ret=mail()
				if ret:
					print("邮件发送成功")
				else:
					print("邮件发送失败")
		else:
			print("执行失败")
browser.close()
browser.quit()#退出浏览器
